/* -*- Mode: Actionscript; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
/**
@author Timothy Uruski 
@version 1.0 2007-10-10.
@description
**/

import com.criticalmass.common.geom.Shape;
import com.criticalmass.common.geom.Fill;
import com.criticalmass.common.geom.SolidFill;
import com.criticalmass.common.geom.GradientFill;

class com.criticalmass.common.geom.Ellipse extends com.criticalmass.common.geom.Shape {
   /*========================================================================
   *  CONSTANTS, DEFAULTS AND PROPERTIES
   =========================================================================*/
   
   
   /*========================================================================
   *  ACCESSORS
   =========================================================================*/
   // Coordinates, size and origin are inherited.
   
   
   /*========================================================================
   *  CONSTRUCTOR, INSTANTIATION CONTROL AND LIFECYCLE
   =========================================================================*/
   public function Ellipse(ARG_x:Number, ARG_y:Number, ARG_w:Number, ARG_h:Number) {
      super.constructor.apply(this, arguments);
   }
   
   
   /*========================================================================
   *  RENDERING
   =========================================================================*/
   public function draw(ARG_target:MovieClip, ARG_origin:String, ARG_fill, ARG_linestyle):MovieClip {
      var TMP_origin:Object = Shape.parseOrigin(ARG_origin);
      // Calculate offsets.
      var TMP_xOffset:Number = 0;
      var TMP_gradXOffset:Number = 0;
      switch(TMP_origin.type) {
         case Shape.TOP_LEFT:
         case Shape.LEFT:
         case Shape.BOTTOM_LEFT:
            TMP_xOffset = width / 2;
            TMP_gradXOffset = 0;
         break;
         case Shape.TOP:
         case Shape.CENTER:
         case Shape.BOTTOM:
            TMP_xOffset = 0;
            TMP_gradXOffset = -width / 2;
         break;
         case Shape.TOP_RIGHT:
         case Shape.RIGHT:
         case Shape.BOTTOM_RIGHT:
            TMP_xOffset = -width / 2;
            TMP_gradXOffset = -width;
         break;
         case Shape.CUSTOM:
         default:
            TMP_xOffset = TMP_origin.x;
            TMP_gradXOffset = TMP_origin.x - (width / 2);
         break;
      }
      var TMP_yOffset:Number = 0;
      var TMP_gradYOffset:Number = 0;
      switch(TMP_origin.type) {
         case Shape.TOP_LEFT:
         case Shape.TOP:
         case Shape.TOP_RIGHT:
            TMP_yOffset = height / 2;
            TMP_gradYOffset = 0;
         break;
         case Shape.LEFT:
         case Shape.CENTER:
         case Shape.RIGHT:
            TMP_yOffset = 0;
            TMP_gradYOffset = -height / 2;
         break;
         case Shape.BOTTOM_LEFT:
         case Shape.BOTTOM:
         case Shape.BOTTOM_RIGHT:
            TMP_yOffset = -height / 2;
            TMP_gradYOffset = -height;
         break;
         case Shape.CUSTOM:
         default:
            TMP_yOffset = TMP_origin.y;
            TMP_gradYOffset = TMP_origin.y - (height / 2);
         break;
      }
      
      // Calculate base coordinates.
      var TMP_x:Number = x + TMP_xOffset;
      var TMP_y:Number = y + TMP_yOffset;
      var TMP_r1:Number = width / 2;
      var TMP_r2:Number = height / 2;
      
      // Determine fill-type.
      if(typeof(ARG_fill) == 'number') {
         ARG_target.beginFill(ARG_fill, 100);
      } else if(ARG_fill instanceof SolidFill) {
         var TMP_fill:Object = SolidFill(ARG_fill);
         ARG_target.beginFill(TMP_fill.color, TMP_fill.alpha);
      } else if(ARG_fill instanceof GradientFill) {
         var TMP_fill:Object = GradientFill(ARG_fill);
         // Massage gradient-matrix values into the origin-offset coordinate-space.
         TMP_fill.matrixXpos += TMP_gradXOffset;
         TMP_fill.matrixYpos += TMP_gradYOffset;
         if(TMP_fill.matrixWidth == 0) TMP_fill.matrixWidth = width;
         if(TMP_fill.matrixHeight == 0) TMP_fill.matrixHeight = height;
         // Re-calculate ratios based on shape width.
         TMP_fill.adjustRatios(width);
         ARG_target.beginGradientFill(TMP_fill.type, TMP_fill.colors, TMP_fill.alphas, TMP_fill.ratios, TMP_fill.matrix);
      } else {
         ARG_target.beginFill(0x000000, 100);
      }
      
      // Send drawing instructions.
      ARG_target.moveTo(TMP_x + TMP_r1, TMP_y);
      ARG_target.curveTo(TMP_x + TMP_r1, Math.tan(Math.PI / 8) * TMP_r2 + TMP_y, Math.sin(Math.PI/4) * TMP_r1 + TMP_x, Math.sin(Math.PI / 4) * TMP_r2 + TMP_y);
      ARG_target.curveTo(Math.tan(Math.PI / 8) * TMP_r1 + TMP_x, TMP_r2 + TMP_y, TMP_x, TMP_r2 + TMP_y);
      ARG_target.curveTo(-Math.tan(Math.PI/8) * TMP_r1 + TMP_x, TMP_r2 + TMP_y, -Math.sin(Math.PI / 4) * TMP_r1 + TMP_x, Math.sin(Math.PI / 4) * TMP_r2 + TMP_y);
      ARG_target.curveTo(-TMP_r1 + TMP_x, Math.tan(Math.PI / 8) * TMP_r2 + TMP_y, -TMP_r1 + TMP_x, TMP_y);
      ARG_target.curveTo(-TMP_r1 + TMP_x, -Math.tan(Math.PI / 8) * TMP_r2 + TMP_y, -Math.sin(Math.PI / 4) * TMP_r1 + TMP_x, -Math.sin(Math.PI / 4) * TMP_r2 + TMP_y);
      ARG_target.curveTo(-Math.tan(Math.PI / 8) * TMP_r1 + TMP_x, -TMP_r2 + TMP_y, TMP_x, -TMP_r2 + TMP_y);
      ARG_target.curveTo(Math.tan(Math.PI / 8) * TMP_r1 + TMP_x, -TMP_r2 + TMP_y, Math.sin(Math.PI / 4) * TMP_r1 + TMP_x, -Math.sin(Math.PI / 4) * TMP_r2 + TMP_y);
      ARG_target.curveTo(TMP_r1 + TMP_x, -Math.tan(Math.PI / 8) * TMP_r2 + TMP_y, TMP_r1 + TMP_x, TMP_y);
      ARG_target.endFill();
      
      return ARG_target;
   }
   
   
}